clear all
close all
clc


%NICTA && Server
addpath('/home/johanna/toolbox/libsvm-3.20/matlab')
addpath('/home/johanna/toolbox/yael/matlab');

%Home
addpath('/media/johanna/HD1T/Toolbox/libsvm-3.20/matlab');
addpath('/media/johanna/HD1T/Toolbox/yael/matlab');

path_range_frames = '/home/johanna/codes/datasets_codes/CMU-MMAC/Brownie/';
%display('Only one RUN, ok ????');
%pause();
%RUN =1;
RUN = 12;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
prompt = 'Number of Gaussians? ';
Ncent = input(prompt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
prompt = 'What is segment length? ';
L = input(prompt);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Ncent = 32;
ORI_DIM = 162;
DIM = 40;%162 originally, 81 after PCA
is_pca = true;
if (is_pca)
    display('Using PCA to reduce data');
end

%display(' ');
%display('Calculating FV for Training');
%FV_cmu_stip_training(Ncent, RUN, ORI_DIM, DIM, is_pca);


Ng = int2str(Ncent);
dim = int2str(DIM);
all_run_acc = [];

 %% TRAINING
% for r=1:RUN
%     fprintf('Training RUN %d \n', r);
%     run = int2str(r);
%     
%     people_train = importdata(strcat('./run', run, '/train_list_run', run, '.dat'));
%     actionNames = importdata('actionNames.txt');
%     n_pe_tr  = length(people_train);
%     n_actions = length(actionNames);
%     
%     data_train = [];
%     labels_train = [];
%     
%     
%    
%     display('Loading Training data');
%     for pe = 1: n_pe_tr
%         
%         for act = 1:n_actions
%             
%             if(is_pca)
%                 load_name = strcat('./run', run,  '/FV_training/pca_FV_', people_train(pe),'_',actionNames(act), '_Ng', Ng, '_dim',  dim, '.txt');
%             else
%                 load_name = strcat('./run', run,  '/FV_training/FV_', people_train(pe),'_',actionNames(act), '_Ng', Ng, '_dim',  dim,  '.txt');
%             end
%             
%             sLoad = char(load_name);
%             if (exist(sLoad))
%                 FV = load(sLoad);
%                 data_train = [data_train FV];
%                 labels_train = [labels_train (act )];
%             end
%             
%         end
%     end
%     
%     data_train = data_train';
%     labels_train = labels_train';
%     display('Training...');
%     model = svmtrain(labels_train, data_train, ['-s 0 -t 0 -b 1' ]);
%     
%     if (is_pca)
%         save_name = strcat('./run', run,  '/pca_svm_model_Ng', Ng, '_dim',  dim  );
%     else
%         save_name = strcat('./run', run,  '/svm_model_Ng', Ng, '_dim',  dim );
%     end
%     
%     display('Saving Model...');
%     sSave= char(save_name);
%     save(sSave, 'model');
%     
% end

%% TESTING.
display(' ');

for r=1:RUN
    run = int2str(r);
    
    fprintf('Testing RUN %d \n', r);
    display('Loading SVM Model'); % loading 'model'
    if (is_pca)
        load_name = strcat('./run', run,  '/pca_svm_model_Ng', Ng, '_dim',  dim);
    else
        load_name = strcat('./run', run,  '/svm_model_Ng', Ng, '_dim',  dim);
    end
    
    sLoad= char(load_name);
    display(sLoad);
    load(sLoad);
    
    
    if (is_pca)

        w  =    load(strcat('./run', run, '/universal_GMM/pca_weights_Ng', Ng, 'dim', dim, '.dat' ));
        mu =    load(strcat('./run', run, '/universal_GMM/pca_means_Ng'  , Ng, 'dim', dim, '.dat'  ));
        sigma = load(strcat('./run', run, '/universal_GMM/pca_covs_Ng'   , Ng, 'dim', dim, '.dat'  ));
        trans_matrix_pca = load(strcat('./run', run, '/universal_GMM/pca_transf_matrix_dim', dim, '.dat' ));
        
    else
        %No existe todavia
        w  =    load(strcat('./run', run, '/universal_GMM/weights_Ng', Ng, '_dim', dim, '.dat' ));
        mu =    load(strcat('./run', run, '/universal_GMM/means_Ng'  , Ng, '_dim', dim, '.dat' ));
        sigma = load(strcat('./run', run, '/universal_GMM/covs_Ng'   , Ng, '_dim', dim, '.dat' ));
    end
    
    
    %L = 15 ;
    ACC = [];
    
    people_test = importdata(strcat('./run', run, '/test_list_run', run, '.dat'));
    actionNames = importdata('actionNames.txt');
    n_pe_te  = length(people_test);
    
    test_info =  cell(n_pe_te,4);
    k=1;
    
    
    for pe = 1:n_pe_te
        %display('Loading data');
        
        %Loading matrix with all features (for all frames)
        display(strcat(people_test(pe)));
        load_name = strcat('./multi_features/feat_', people_test(pe),'.dat');
        sLoad = char(load_name);
        feat_video = load(sLoad);
        
        
        if (is_pca)
            %size( trans_matrix_pca );
            %size(feat_video);
            feat_video = feat_video'*trans_matrix_pca;
            feat_video = feat_video';
            %size(feat_video);
            %pause()
        end
        
        %Loading labels. In a frame basis
        load_name_lab = strcat('./multi_features/lab_', people_test(pe),'.dat');
        sLoad_lab = char(load_name_lab);
        real_labels = load(sLoad_lab);
        est_labels  = ones (length(real_labels),1)*(-1);
        
        %Loading frame index for each of the feature vector in feat_video
        load_name_fr_idx= strcat('./multi_features/fr_idx_', people_test(pe),'.dat');
        sLoad_fr_idx = char(load_name_fr_idx);
        fr_idx = load(sLoad_fr_idx);
        fr_idx = fr_idx +1 ; %Starts at 0 in c++
        %display('termina');
        %pause()
        
        %Loading ini and end frame
        %load_range = strcat(path_range_frames, people_test(pe), '_start_end.dat');
        %range = load( char( load_range ) );
        %fr_idx = fr_idx + 2 - range(1);
        
        ini = 1;
        fin = ini + L;
        more = true;
        while(more) %
            
            [pre_label pre_prob_label ini fin] = classify_segment_stip(ini, fin, L, fr_idx, feat_video, w, mu, sigma, model);
            
            fin = fin + 1;
            
            [label prob_label ini fin] = classify_segment_stip(ini, fin, L, fr_idx, feat_video, w, mu, sigma, model);
            
            while (label == pre_label) && ( prob_label> pre_prob_label) && ( fin+1< fr_idx(end))
                %display([ini fin]);
                pre_label = label;
                pre_prob_label = prob_label;
                fin = fin + 1;
                [label prob_label ini fin] = classify_segment_stip(ini, fin, L, fr_idx, feat_video, w, mu, sigma, model);
            end
            
            if (ini >= fr_idx(end) )
                more =false;
                
            elseif ( fin > fr_idx(end) )
                
                fin = fr_idx(end);
                [label prob_label ini fin] = classify_segment_stip(ini, fin, L, fr_idx, feat_video, w, mu, sigma, model);
                est_labels (ini:fr_idx(end)) = label;
                more =false;
            else
                est_labels (ini:fin) = pre_label;
                ini = fin;
                fin = ini + L;
                %display('Que caso es este???');
            end
            
        end
        
        is_negative = est_labels <0;
        is_negative = est_labels(is_negative);
        if (length(is_negative) > 0 )
            display('Que pasa Aqui. Pq son negativos???') ;
            %pause
        end
        
        acc = sum(est_labels == real_labels)*100/length(real_labels);
        ACC =  [ACC acc]
        test_info{k,1} = strcat(people_test(pe));
        test_info{k,2} = real_labels;
        test_info{k,3} = est_labels;
        test_info{k,4} = acc;
        
        k = k+1;
    end
    
    display('Saving results');
    
    save_info = strcat('./run', run, '/test_info_ONEsvm_Ng', Ng, '_dim',  dim, '.mat');
    
    if (is_pca)
        save_info = strcat('./run', run, '/pca_test_info_ONEsvm_Ng', Ng, '_dim',  dim,  '.mat');
    end
    
    sSave_info= char(save_info);
    save(sSave_info, 'test_info');
    
    all_run_acc = [all_run_acc ACC];
end


